package com.fr.lintcode;

public class Q1292 {
    /**
     * @param head:
     *            a singly linked list
     * @return: Modified linked list
     */
    public ListNode oddEvenList(ListNode head) {
        // write your code here
        if (head == null) {
            return null;
        }
        ListNode node = head;
        ListNode node1 = head.next;
        ListNode node2 = null;
        while (node != null) {
            ListNode tmp = node.next;
            if (tmp == null) {
                node.next = node1;
                if (node2 != null) {
                    node2.next = null;
                }
                break;
            }
            ListNode tmp2 = tmp.next;
            if (node2 == null) {
                node2 = tmp;
            } else {
                node2.next = tmp;
            }
            node2 = tmp;
            if (tmp2 == null) {
                node.next = node1;
            } else {
                node.next = tmp2;
            }

            node = tmp2;
        }
        return head;
    }

}
